/**
 * 
 */
package com.seadragon.app.ksr.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.seadragon.app.ksr.model.Address;
import static com.seadragon.app.ksr.model.KsrConstants.ERROR_MESSAGE_KEY_FIELD_REQUIRED;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_ADDRESSLINE1;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_CITY;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_STATE;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_ZIP;
/**
 * @author yadong
 *
 */
public class AddressValidator implements Validator {

	public boolean supports(Class<?> clazz) {
		return Address.class.isAssignableFrom(clazz);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	public void validate(Object target, Errors errors) {
		Address add = (Address) target;
		if (StringUtils.isEmpty(add.getAddressLine1()))
			errors.reject(FIELD_NAME_ADDRESSLINE1,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(add.getCity()))
			errors.reject(FIELD_NAME_CITY,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(add.getState()))
			errors.reject(FIELD_NAME_STATE,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(add.getZip()))
			errors.reject(FIELD_NAME_ZIP,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
	}

}
